package android.bignerdranch.xiangmu;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class QDscore extends AppCompatActivity implements View.OnClickListener{
    private static final String PREFS_NAME ="android.bignerdranch.xiangmu" ;
    int counted = 0;
    private MyHelper myHelper;
    List<Map<String, Object>> lists;
    SimpleAdapter adapter;
    Button bt_save,bt_clear,uesBtn;
    int count = 0;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    SQLiteDatabase db;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qdscore);
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.apply();
        myHelper = new MyHelper(this);
        db = myHelper.getWritableDatabase();
        bt_clear = findViewById(R.id.clear_score);
        bt_save = findViewById(R.id.save_score);
        bt_save.setOnClickListener(this);
        bt_clear.setOnClickListener(this);
        count = sharedPreferences.getInt("count",0);
        lists = new ArrayList<>();

        Cursor c = db.query("information", null, null,
                null, null, null, null);
        while(c.moveToNext()){
            Map<String, Object> map = new HashMap<>();
            String name = c.getString(c.getColumnIndex("name"));
            String id = c.getString(c.getColumnIndex("id"));
            String student_class = c.getString(c.getColumnIndex("class"));
            String state =c.getString(c.getColumnIndex("state"));
            map.put("name", "姓名:"+name);
            map.put("id", "学号:"+id);
            map.put("classes", "班级:"+student_class);
            map.put("Button",state);
            lists.add(map);
        }
        /***for (int i = 1; i < count; i++) {
            Map<String, Object> map = new HashMap<>();
            String meg_name = "Student"+i+"name";
            String meg_id = "Student"+i+"id";
            String meg_classes = "Student"+i+"classes";
            map.put("name", "姓名:"+sharedPreferences.getString(meg_name,null));
            map.put("id", "学号:"+sharedPreferences.getString(meg_id,null));
            map.put("classes", "班级:"+sharedPreferences.getString(meg_classes,null));
            map.put("Button","已到");
            lists.add(map);
        }***/
        adapter = new SimpleAdapter(QDscore.this, lists, R.layout.list, new String[]{"name", "id", "classes","Button"}, new int[]{R.id.text1, R.id.text2, R.id.text3,R.id.is_listen})
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final View view=super.getView(position, convertView, parent);
                Button useBtn=(Button)view.findViewById(R.id.is_listen);

                useBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String id = lists.get(position).get("id").toString();
                        String REGEX = "[^0-9]";
                        String stu_id = Pattern.compile(REGEX).matcher(id).replaceAll("").trim();
                        ContentValues values = new ContentValues();
                        System.out.println(stu_id);
                        if(counted ==0) {
                            useBtn.setText("迟到");
                            values.put("state","迟到");
                            db.update("information",values,"id=?",new String[]{stu_id});
                            counted++;
                        }
                        else if(counted == 1)
                        {
                            useBtn.setText("未到");
                            values.put("state","未到");
                            db.update("information",values,"id=?",new String[]{stu_id});
                            counted = 0;
                        }


                    }
                });
                return view;
            }
        };

        ListView listView = (ListView) findViewById(R.id.stu_list);
        listView.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        final int bt_save = R.id.save_score;
        final int bt_clear = R.id.clear_score;
        switch (v.getId())
        {
            case bt_clear:
            {
                ListView listView = (ListView) findViewById(R.id.stu_list);
                listView.setAdapter(adapter);
                Toast.makeText(QDscore.this,"签到表已重置",Toast.LENGTH_SHORT).show();
                break;
            }
            case bt_save:{
                for (int i=1;i<count;i++)
                {
                    ContentValues values = new ContentValues();

                }

                Toast.makeText(QDscore.this,"签到表已保存",Toast.LENGTH_SHORT).show();
                break;
            }
        }

    }
}
